package com.itheima;

import com.alibaba.fastjson.JSON;
import com.itheima.pojo.User;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 文档的增删改查
 * @作者 itcast
 * @创建日期 2020/11/3 9:00
 **/
public class EsDemo02 {
    RestHighLevelClient client;

    @Test
    public void addDocument() throws IOException {
        // 要添加的文档数据
        User user = new User(1L,"鲁班",22,"1","鲁班在河道唱着歌");
        // 1.  创建一个请求对象   POST user/_doc/1    { json }
        IndexRequest request = new IndexRequest("user"); // 指定索引库
        request.id(user.getId()+""); // 指定文档的ID
        String userJson = JSON.toJSONString(user);
        request.source(userJson,XContentType.JSON); // 构建文档json数据
        // 2.  调用client执行对应请求
        IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
        // 3.  解析请求结果
        System.out.println(indexResponse.getResult());
    }

    @Test
    public void getDocumentById() throws IOException {
        // 1. 创建请求
        GetRequest request = new GetRequest("user","1");
        // 2. 执行请求
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        // 3. 解析结果
        String userJson = response.getSourceAsString(); // 获取source字段时 得到的是文档的json字符串

        User user = JSON.parseObject(userJson, User.class);// 通过fastjson 反序列化成java对象

        System.out.println(user);
    }

    @Test
    public void updateById() throws IOException {
        // 1. 创建请求
        UpdateRequest request = new UpdateRequest("user","1");
        // 2. 执行请求   修改方法 可以指定修改某个字段的值
        request.doc("gender","0");
        // 3. 解析结果
        UpdateResponse update = client.update(request, RequestOptions.DEFAULT);
        System.out.println(update.getResult());
    }

    @Test
    public void deleteById() throws IOException {
        DeleteRequest request = new DeleteRequest("user","1");
        DeleteResponse delete = client.delete(request, RequestOptions.DEFAULT);
        System.out.println(delete.getResult());
    }


    @Test
    public void bulkAddDocuments() throws IOException {
        // 要添加的数据
        List<User> userList = Arrays.asList(
                new User(1L, "Rose", 18, "1", "Rose同学在传智播客学表演"),
                new User(2L, "Jack", 38, "1", "Jack同学在黑马学JavaEE"),
                new User(3L, "小红", 23, "0", "小红同学在传智播客学唱歌"),
                new User(4L, "小明", 20, "1", "小明同学在黑马学JavaSE"),
                new User(5L, "达摩", 33, "1", "达摩和尚在达摩院学唱歌"),
                new User(6L, "鲁班", 24, "1", "鲁班同学走在乡间小路上"),
                new User(7L, "孙尚香", 26, "0", "孙尚香同学想带阿斗回东吴"),
                new User(8L, "李白", 27, "1", "李白同学在山顶喝着酒唱着歌"),
                new User(9L, "甄姬", 28, "0", "甄姬同学弹奏一曲东风破"),
                new User(10L, "虞姬", 27, "0", "虞姬同学在和项羽谈情说爱")
        );
//        1. 创建 bulk请求对象
        BulkRequest request = new BulkRequest("user");
        for (User user : userList) {
            IndexRequest indexRequest = new IndexRequest();
            indexRequest.id(user.getId() + ""); // 设置ID
            indexRequest.source(JSON.toJSONString(user),XContentType.JSON); // 设置文档JSON字符串
            request.add(indexRequest);
        }
//        2. 调用客户端执行 bulk请求
        BulkResponse bulk = client.bulk(request, RequestOptions.DEFAULT);
//        3. 解析请求结果
        System.out.println(bulk.status());
    }

    @Before
    public void init(){
        // 构建ES的客户端
        client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("localhost",9200,"http")
                )
        );
    }
    @After
    public void close(){
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
